package server.core.base;

import java.util.HashSet;
import java.util.Set;

import server.core.manager.Admin;
import server.core.manager.Customer;
import server.core.manager.Guest;
import server.core.manager.Manager;

public class User extends EntityBase {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 7515647213844071123L;

	public enum Rule {
		ViewFlight(1, Guest.class),// A Guest, view information about flights and tickets
		ReserveTicket(2, Customer.class),//A Customer, choose tickets
		EditFlight(4 ,Admin.class),//A Staff, edit flights
		EditBase(8 ,Admin.class),//A Staff, edit base(airport,airplane,terrain)
		EditUser(16 ,Admin.class),//A Administrator, edit user account
		GetDao(32 ,Admin.class),//for get full DAO
		SetDao(64 ,Admin.class);//for set all DAO
		private int num;
		private Class<? extends Manager> manager;
		Rule(int num, Class<? extends Manager> class1){
			this.setNum(num);
			this.setManager(class1);
		}
		public int getNum() {
			return num;
		}
		public void setNum(int num) {
			this.num = num;
		}
		public Class<? extends Manager> getManager() {
			return manager;
		}
		public void setManager(Class<? extends Manager> manager) {
			this.manager = manager;
		}
	}
	
	private String name;
	public void setName(String name){
		if((name==null)||("".equals(name)))
			throw new communication.exception.EmptyNameException();
		this.name=name;
	}
	public String getName(){
		return name;
	}
	
	private String password;
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	private int ruleMask;
	public int getRuleMask() {
		return ruleMask;
	}
	public void setRuleMask(int ruleMask) {
		this.ruleMask = ruleMask;
	}
	
	private Set<Class<? extends Manager>> managers;
	public boolean contentsManager(Class<? extends Manager> class1){
		if(managers.contains(class1))
			return true;
		else
			return false;
	}

	public User(String name,String password,int ruleMask){
		setName(name);
		setPassword(password);
		setRuleMask(ruleMask);
		this.managers=new HashSet<Class<? extends Manager>>();
		for(Rule rule : Rule.values()){
			if(rule.getNum()==(ruleMask & rule.getNum()))
				this.managers.add(rule.getManager());
		}
	}
	
	public String toString(){
		return "id="+getId()+" name="+getName()+" ruleMask="+Integer.toBinaryString(getRuleMask());
	}

}
